﻿using System;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Regions;

namespace SimpleList.Infrastructure.Behaviors
{
        [Export(typeof(AutoPopulateExportedViewsBehavior))]
        [PartCreationPolicy(CreationPolicy.NonShared)]
        public class AutoPopulateExportedViewsBehavior : RegionBehavior, IPartImportsSatisfiedNotification
        {
            protected override void OnAttach()
            {
                AddRegisteredViews();
            }

            public void OnImportsSatisfied()
            {
                AddRegisteredViews();
            }

            private void AddRegisteredViews()
            {
                if (this.Region != null)
                {
                    foreach (var viewEntry in this.RegisteredViews)
                    {
                        if (viewEntry.Metadata.RegionName == this.Region.Name)
                        {
                            var view = viewEntry.Value;

                            if (!this.Region.Views.Contains(view))
                            {
                                this.Region.Add(view);
                            }
                        }
                    }
                }
            }

            [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "MEF injected values"), ImportMany(AllowRecomposition = true)]
            public Lazy<object, IViewRegionRegistration>[] RegisteredViews { get; set; }
        }
}
